package ch.usi.inf.pf2.cell;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.border.Border;

/**
 * @author Ebrisa Savina Mastrodicasa (Lorenzo Baracchi only for changes)
 * @date April 17th, 2010
 */

/**
 * Cell class defines the minimal object that constructs the entire grid. Each
 * cell can be filled up with some content.
 */
public class Cell {

	private Data content;
	private Color color;
	private Border borderType;
	private int cellLength;

	/**
	 * Construct a white default Cell object with no data inserted and none
	 * border.
	 */
	public Cell() {
		content = null;
		color = Color.WHITE;
		borderType = BorderFactory.createEmptyBorder();
		cellLength = 8;
	}

	/**
	 * @return The content of the data in the cell.
	 */
	public Data getContent() {
		return content;
	}

	/**
	 * Set the content field to the given data.
	 * 
	 * @param content
	 *            The Data content to be inserted into the cell.
	 */
	public void setContent(Data content) {
		this.content = content;
	}

	/**
	 * Set the content field to the given value.
	 * 
	 * @param content
	 *            The String content to be inserted into the cell.
	 */
	public void setContent(String content) {
		this.content = Data.createData(content);
	}

	/**
	 * @return The color of the cell.
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * Set the color field to the given color.
	 * 
	 * @param color
	 *            The color wanted for the cell.
	 */
	public void setColor(Color color) {
		this.color = color;
	}

	/**
	 * @return The border type of the cell.
	 */
	public Border getBorderType() {
		return borderType;
	}

	/**
	 * Set the borderType field to the given type.
	 * 
	 * @param borderType
	 *            The border type to be set.
	 */
	public void setBorderType(Border borderType) {
		this.borderType = borderType;
	}

	/**
	 * @return The length of the cell.
	 */
	public int getCellLength() {
		return cellLength;
	}

	/**
	 * Set the cellLength field to the given size.
	 * 
	 * @param cellLength
	 *            The length to be set.
	 */
	public void setCellLength(int cellLength) {
		this.cellLength = cellLength;
	}

	/**
	 * Cut the content inserted in the cell according to the length of the cell
	 * itself.
	 * 
	 * @return The String really printed into the cell.
	 */
	public String cutContentAccordingCellLength() {
		if (content.toString().length() > cellLength) {
			return content.toString().substring(0, cellLength);
		} else {
			if (content.toString().length() == cellLength) {
				return content.toString();
			} else {
				String toBePrinted = content.toString();
				for (int i = content.toString().length(); i < cellLength; i++) {
					toBePrinted += " ";
				}
				return toBePrinted;
			}
		}
	}

	public String toString() {
		return getContent().toString();
	}

}
